<?php
    
    class LMysql extends LDatabase{
        
        private $connection;
        private $query;
        private $sresult;
        
        public function __construct(){
           // echo __CLASS__;
        } 
        
        // connect to mysql
        public function connect(){
            
            try{
                
                $this->connection  = new mysqli(LConfig::DBHOST, LConfig::DBUSER, LConfig::DBPASSWORD, LConfig::DBNAME, LConfig::DBPORT, LConfig::DBSOCKET);
                if(mysqli_connect_error()){
                    print mysqli_connect_error();
                }else{
                   
                }
                return true;
                
            }
            catch(Exception $e){
                die($e->getMessage());
            }
            
        }
        
        // disconnect from mysql
        public function disconnect(){
            $this->connection->close();
            return true;
        }
        
        // prepare query
        public function prepare($query){
            //print $query;
            $this->query = $query;
            return true;
        }
        
        // execute query
        public function query(){
            
            try{
                if(isset($this->query)){
                    $this->sresult = $this->connection->query($this->query) or
                    die(mysqli_error());
                    return true;
                }
                return false;
            }
            catch(Exception $e){
                die($e->getMessage());
            }
            
        }
        
        // get result
        public function fetch($result = 'object'){
            $n = 0;
            $rows = array();
            try{
                if(isset($this->sresult)){
                    //print $result;
                    switch($result){
                        case 'array' : 
                            while($row = $this->sresult->fetch_array(MYSQLI_ASSOC)){
                                $rows[$n] = $row;
                                $n++;
                            }
                        break;
                        case 'object' :
                        default : $rows = $this->sresult->fetch_object(); break;
                    }
                    return $rows;
                }
                return false;
            }
            catch(Exception $e){
                die("Error" . $e->getMessage());
            }
        }
        
    }
    
?>